package com.dh.blog.handler.xml;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.dh.blog.utils.PatternInjectUtils;

public class XmlNode implements CTNode,Serializable {
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -3809563906731470142L;
	public static final String INDEX_KEY = "id";
	private CTNode parent = null;
	private int level = 0;
	private int offset = 0;
	private String text = null;
	private CTNode[] childs = new CTNode[2];
	private int childlen = 0;
	private XmlTemplates template = XmlTemplates.LI_LINK_CATALOG;
	private Map<String,Object> attrs = new HashMap<String,Object>();
	public XmlNode(){
	}
	public XmlNode(int level,int offset){
		this.level = level;
		this.offset = offset;
	}
	public int offset() {
		return offset;
	}
	public void setOffset(int offset) {
		this.offset = offset;
	}
	public void setTemplate(XmlTemplates template){
		this.template = template;
	}
	public XmlTemplates getTemplate(){
		return this.template;
	}
	public int level() {
		return level;
	}
	public void setLevel(int level) {
		this.level = level;
	}
	public CTNode getParent() {
		return parent;
	}
	public void setParent(CTNode node) {
		this.parent = node;
	}
	public CTNode[] getChilds() {
		return childs;
	}
	public void setChilds(CTNode[] childs){
		this.childs = childs;
	}
	public void addChilds(CTNode[] nodes) {
		if(null == this.childs){
			this.childs = nodes;
			childlen = nodes.length;
		}else{
			int ext = this.childs.length - childlen;
			if(ext>nodes.length){
				System.arraycopy(nodes,0,this.childs,childlen,nodes.length);
				childlen +=nodes.length;
			}else{
				int len = nodes.length + childlen;
				CTNode[] results = new CTNode[len];
				System.arraycopy(this.childs, 0,results, 0,childlen);
				System.arraycopy(nodes,0,results,childlen,nodes.length);
				this.childs = results;
				childlen = results.length;
			}
		}
	}
	public void addChild(CTNode child) {
		addChilds(new CTNode[]{child});
	}
	public void removeChild(int i){
		if(i == childs.length -1){
			childs[i] = null;
		}else{
			for(int j = childlen-1;j>i;j--){
				childs[j-1] = childs[j];
			}
		}
		childlen-=1;
	}
	public void setText(String text) {
		this.text = text;
	}
	public String getText() {
		return text;
	}
	public void addAttr(String key, Object value) {
		attrs.put(key, value);
	}
	public void removeAttr(String key) {
		attrs.remove(key);
	}
	public Object getAttr(String key) {
		return attrs.get(key);
	}
	public Map<String, Object> attrs() {
		return null;
	}
	public int childNum(){
		return childlen;
	}
	public void update(CTNode n) {
		this.attrs = n.attrs();
		this.text = n.getText();
	}
	public String getIndex() {
		return String.valueOf(getAttr(INDEX_KEY));
	}
	public String serialize() {
		//可能造成影响
		StringBuilder result = new StringBuilder();
		if(null != text){
			String prefix = null;
			if(null == (prefix = (String) attrs.get("prefix"))){
				StringBuilder pBuilder = new StringBuilder();
				for(int i = 0;i<String.valueOf(attrs.get(INDEX_KEY)).length()-2;i++){
					pBuilder.append("...");
				}
				prefix = pBuilder.toString();
			}
			attrs.put("text", text);
			attrs.put("prefix", prefix);
			result.append(PatternInjectUtils
					.inject(template.getValue(),attrs));
		}
		for(int i = 0;i<childlen;i++){
			result.append("\n"+childs[i].serialize());
		}
		return result.toString();
	}
}
